/**
 * @description: 文件描述
 * @version: 0.0.1
 * @author: xxx
 * @Date: 2025-03-14 14:07:15
 */
import { ref } from "vue";
import { useStore } from '@/store/useState'
import { useRouter } from 'vue-router'
import { login } from "../utils/api.js"
import element from '@/utils/element.js';
export function useAuth(form) {
    const loading = ref(false)
    const userStore = useStore()
    const router = useRouter()
    const userLogin = async () => {
        loading.value = true
        try {
            if (form.username && form.password) {
                const res = await login(form);
                if (res && res.code === 200 && res.data.token) {
                    element.toast('登录成功','success')
                    userStore.setToken(res.data.token)
                    router.push('/home')
                } else {
                    element.toast('登录失败','error')
                }
            } else {
                element.toast('请输入用户名和密码','warning')
            }
        } catch (error) {
            console.log(error, 'userLogin')
        } finally {
            loading.value = false
        }
    }
    return { loading, userLogin }
}